﻿using System;
using CustomerContactSystem.Core.AppServices.Email;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms
{
    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetDepartments();
            }
        }

        private void GetDepartments()
        {
            var departments = DepartmentRepository.QueryOver().OrderBy(d => d.DepartmentName).Asc;
            BindDropDownList(DeptList, departments.List(), "DepartmentName", "Id", "--Select Department--", "0");
        }

        protected void RegisterUserClick(object sender, EventArgs e)
        {
            if (!IsValidRegistration()) {
                return;
            }

            var u = new User
                {
                    Department = DepartmentRepository.LoadReference(int.Parse(DeptList.SelectedItem.Value)),
                    UserGroup = UserGroupRepository.LoadReference(ConfigSettings.DefaultGroupId),
                    AreaCovered = "",
                    Username = Username.Text.Trim(),
                    DisplayName = DisplayName.Text.Trim(),
                    Password = Password.Text.Trim(),
                    Email = Email.Text.Trim(),
                    IsActive = true
                };
            UserRepository.Add(u);

            u.AddRole(UserRoleRepository.LoadReference(ConfigSettings.DefaultRoleId));
            UserRepository.Update(u);

            var loginInfoEmail = new LoginDetailsEmail(ConfigSettings.AdminEmailAddress,
                                                       u.Email, u.Username,
                                                       u.Password);

            //var smtpService = new SmtpEmailService(ConfigSettings.GetStringSetting("SmptServerAddress"));
            EmailService.SendEmail(loginInfoEmail);
                
            AddUserToSession(u.Id);
            Response.Redirect("~/Enquiries/Search.aspx");
        }

        private bool IsValidRegistration()
        {
            if (DeptList.SelectedIndex == 0)
            {
                ErrorMsg.Text = "Please select your department!";
                return false;
            }

            if (Username.Text.Trim().Length == 0)
            {
                ErrorMsg.Text = "Please choose a username!";
                return false;
            }

            if (UserRepository.QueryOver().Where(u => u.Username == Username.Text).SingleOrDefault() != null)
            {
                ErrorMsg.Text = "This Username is already in use!";
                return false;
            }

            if (DisplayName.Text.Length == 0)
            {
                ErrorMsg.Text = "Please select your display name for the system!";
                return false;
            }

            if (Email.Text.Trim().Length == 0)
            {
                ErrorMsg.Text = "Please enter your email address!";
                return false;
            }
            
            if (Email.Text.Trim()!= EmailAgain.Text.Trim())
            {
                ErrorMsg.Text = "Emails do not match!";
                return false;
            }

            if (Password.Text.Length == 0)
            {
                ErrorMsg.Text = "Please enter your chosen password and then verify it by entering it again!";
                return false;
            }
            
            if (Password.Text != Password2.Text)
            {
                ErrorMsg.Text = "Passwords do not match!";
                return false;
            }

            ErrorMsg.Text = "";
            return true;
        }
    }
}